<?php
class Pwd extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		// 登录校验
		$this->common->check_login_admin ();
		$this->load->helper ( array (
				'form',
				'url' 
		) );
	}
	
	// 首页
	public function pwd() {
		$this->load->model ( 'Admin_model' );
		
		$sql = "SELECT * FROM pc_ddth_admin WHERE id = ".$_SESSION ['admin'] ['id'];
		$query = $this->db->query ( $sql );
		$query = $query->row_array ();
		$data ['left'] = $this->load->view ( 'admin/left', '', TRUE );
		$data ['menu'] = $this->load->view ( 'admin/menu', '', TRUE );
		
		$this->load->view ( 'admin/pwd', $data );
	}
	
	/**
	 * 修改信息
	 */
	public function update_pwd() {
		if($_POST ['oldpwd']==''){
			exit ( $this->common->alert_msg_url ( '请填写旧密码', site_url ( 'admin/pwd/pwd' ) ) );
		}
		if($_POST ['pwd']==''){
			exit ( $this->common->alert_msg_url ( '请填写新密码', site_url ( 'admin/pwd/pwd' ) ) );
		}
		if($_POST ['pwd']!=$_POST ['rpwd']){
			exit ( $this->common->alert_msg_url ( '两次填写的新密码不同', site_url ( 'admin/pwd/pwd' ) ) );
		}
		if($_POST ['pwd']===$_POST ['oldpwd']){
			exit ( $this->common->alert_msg_url ( '新密码不能与旧密码相同', site_url ( 'admin/pwd/pwd' ) ) );
		}
		
		$this->load->model ( 'Admin_model' );
		
		$sql = "SELECT * FROM pc_ddth_admin WHERE id = ".$_SESSION ['admin'] ['id'];
		$query = $this->db->query ( $sql );
		$query = $query->row_array ();
		if(md5($_POST ['oldpwd'])!=$query['pwd'] ){
			exit ( $this->common->alert_msg_url ( '原密码错误', site_url ( 'admin/pwd/pwd' ) ) );
		}
		
		$data = array (
				'pwd' => md5($_POST ['pwd']),
				'spwd' => md5($_POST ['pwd'])
		);
		
		$result = $this->db->update ( 'pc_ddth_admin', $data, array (
				'id' => $_SESSION ['admin'] ['id'] 
		) );
		
		
		if ($result) {
			exit ( $this->common->alert_msg_url ( '修改成功', site_url ( 'admin/pwd/pwd' ) ) );
		} else {
			exit ( $this->common->alert_msg_goback ( '修改失败' ) );
		}
	}
	
}